﻿
Imports KUeLib.Attachments
Imports KUeLib.Data
Imports CoorpDesignLib
Imports System.Windows.Forms
Imports System.Drawing
Imports KUeLib.Data.ServiceDefinitions

Namespace Formulare.Fileattachments

    Public Class FileAttachmentForm

        Private _fileAttachmentService As FileAttachmentDataService = _
                            ServiceManager.GetInstance().GetFileAttachmentDataService()

        Private _processId As Integer

        Private _errorProvider As New ErrorProvider

        Public Overloads Sub ShowDialog(ByVal processId As Integer)
            _processId = processId
            MyBase.ShowDialog()
        End Sub

        Private Sub FileAttachmentForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ControlFormater.GetInstance().RegisterControls(Me.Controls)
        End Sub

        Private Sub FileAttachmentForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawRectangle(Pens.Black, 0, 0, Me.Width - 1, Me.Height - 1)
        End Sub

        Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Me.Close()
        End Sub

        Private Sub BtnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpenFile.Click
            Dim ofd As New OpenFileDialog
            ofd.Multiselect = False
            If ofd.ShowDialog() = DialogResult.OK Then
                tbFile.Text = ofd.FileName
            Else
                tbFile.Text = Nothing
            End If
        End Sub

        Private Sub BtnBestaetigen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBestaetigen.Click
            Dim errorFound As Boolean = False
            _errorProvider.Clear()

            If tbBezeichnung.Text = "" OrElse tbBezeichnung.Text Is Nothing Then
                _errorProvider.SetError(tbBezeichnung, "Bitte geben Sie eine Bezeichnung für die Datei an.")
                errorFound = True
            End If

            If tbFile.Text = "" OrElse tbFile.Text Is Nothing Then
                _errorProvider.SetError(BtnOpenFile, "Bitte wählen Sie erst eine Datei aus.")
                errorFound = True
            End If

            If Not errorFound Then
                Dim fi As New IO.FileInfo(tbFile.Text)
                Dim fa As New FileAttachment

                fa.ProcessId = _processId
                fa.Bezeichnung = tbBezeichnung.Text
                fa.Filename = fi.FullName.Substring(fi.FullName.LastIndexOf("\") + 1)
                fa.Datum = Date.Now.Date
                fa.File = IO.File.ReadAllBytes(fi.FullName)
                Try
                    _fileAttachmentService.InsertNewFileAttachment(fa)
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Datenbankfehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End Try

                Me.Close()
            End If

        End Sub

    End Class

End Namespace